package chatv2.server;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.HashMap;

import rmioverlay.lib.users.User;
import rmioverlay.lib.users.UserList;
import rmioverlay.server.Server;

public class ServerMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			String loginsFile = "src/chatv2/server/logins.txt";
			String chaine = "";
			ChatServerApp serverApp = new ChatServerApp();
			Server server = new Server(serverApp);
			HashMap users = new HashMap<String, User>();
			
			try{
				InputStream ips= new FileInputStream(loginsFile); 
				InputStreamReader ipsr=new InputStreamReader(ips);
				BufferedReader br=new BufferedReader(ipsr);
				
				String ligne;
				while ((ligne=br.readLine())!=null){
					String[] split =ligne.split(" ");
					String login = split[0];
					String password = split[1];		
					
					users.put(login, new User(login, password));
					//System.out.println(login + password);
				}
				
				br.close(); 
			}			
			catch (Exception e) {
				e.printStackTrace();
			}
		
			UserList.setUserList(users);
			
			server.start();
			
			System.out.println("Chat Server running.");
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}
